import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import chalk from 'chalk'

chalk.level = 1

const msgPath = resolve('./.git/COMMIT_EDITMSG')
const msg = readFileSync(msgPath, { encoding: 'utf8' }).trim()

const commitRE = /^(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\(.+\))?: .{1,50}/

if (!commitRE.test(msg)) {
  console.log()
  console.error(
    `${chalk.bgRed.white('ERROR')} ${chalk.red('无效的提交信息')}
    
    ${chalk.bgGreen.white('eg:')}
    ${chalk.green('  feat(uniapp): add some functions')}
    ${chalk.green('  fix(vue3): xx page bug (close #28)')}

    ${chalk.red('  See .github/commit-convention.md for more details.\n')}`,
  )
  process.exit(1)
}
